/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.definitions;

/**
 *
 * @author abracadabuda
 */
public class ReferenceDefinition extends AbstractDefinition {
    private String name;
    private String catalog;
    private String schema;
    private String tblName;
    private String colName;
    private String updateRule;
    private String deleteRule;
    private String defferable;
    
    private ColumnDefinition column;
    
    public ReferenceDefinition(ColumnDefinition column) {
        this.column = column;
        schema = AbstractDefinition.prefix;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getTblName() {
        return tblName;
    }

    public void setTblName(String tblName) {
        this.tblName = tblName;
    }

    public String getColName() {
        return colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

    public String getUpdateRule() {
        return updateRule;
    }

    public void setUpdateRule(String updateRule) {
        this.updateRule = updateRule;
    }

    public String getDeleteRule() {
        return deleteRule;
    }

    public void setDeleteRule(String deleteRule) {
        this.deleteRule = deleteRule;
    }

    public String getDefferable() {
        return defferable;
    }

    public void setDefferable(String defferable) {
        this.defferable = defferable;
    }
    
    /**
     * prints the dump of reference (for testing)
     */
    public void getDump() {
        System.out.println("\tReference:");
        System.out.println("\t\tName: " + name);
        System.out.println("\t\tCatalog: " + catalog);
        System.out.println("\t\tSchema: " + schema);
        System.out.println("\t\tTable: " + tblName);
        System.out.println("\t\tColumn: " + colName);
        System.out.println("\t\tLocal table: " + column.getTable().getName());
        System.out.println("\t\tLocal column: " + column.getName());        
        System.out.println("\t\tUpdate rule: " + updateRule);
        System.out.println("\t\tDelete rule: " + deleteRule);
        System.out.println("\t\tDefferable: " + defferable);
    }

    public ColumnDefinition getColumn() {
        return column;
    }
    
    /**
     * returns SQL code for reference definition (ALTER TABLE..)
     * @return SQL code
     */
    public String getSQL() {
        String sql = new String();
        sql += "ALTER TABLE \"";
        sql += getColumn().getTable().getSchema() + "\".";
        sql += "\"" + getColumn().getTable().getTblName() + "\" " ;
        sql += "ADD CONSTRAINT \"" + name + "\" FOREIGN KEY (\"" + getColumn().getName() + "\") \n";
        sql += "REFERENCES ";
        sql += schema.equals("") ? "" : "\"" + schema + "\".";
        sql += "\"" + tblName + "\"(\"" + colName + "\")\n";
        sql += "MATCH SIMPLE\n";
        sql += "ON DELETE " + deleteRule + "\n";
        sql += "ON UPDATE " + updateRule + "\n";
        sql += defferable;
        return sql + ";\n";
    }
}
